//
//  AppDelegate.swift
//  WB01
//
//  Created by HOP on 15/12/15.
//  Copyright © 2015年 HOP. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        //设置导航栏按钮皮肤
        setupAppearance()
// TODO: 加载账号,能加载到账号表示用户登录了,加载不到账号表示用户没有登录
        let userAccount = HOPuserAccount.loadUserAccount()
        print("加载账号: userAccount: \(userAccount)")
        //创建窗口
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        // 设置window的背景颜色
        window?.backgroundColor = UIColor.whiteColor()
//        指定根控制器
        
        window?.rootViewController = defaultViewController()
//        显示窗口
        window?.makeKeyAndVisible()
        // 测试判断新版本功能
        let newVersion = isNewVersion()
        print("newVersion： \(newVersion)")
        return true
    }
    
//    / 目标控制器有2个
    // 谁来赋值切换AppDelegate,切换界面其实就是切换window的跟控制器.window是AppDelegate的一个属性,AppDelegate来负责切换最合适
    /// 切换程序的根控制器, isMain = true 表示切换到MainViewController, isMain = false 表示切换到WelcomeViewController
    func switchRootViewController(isMain: Bool) {
        window?.rootViewController = isMain ? HOPMainController() : HOPWelcomeController()
     }
    
     //设置导航栏按钮皮肤
    func setupAppearance() {
    UINavigationBar.appearance().tintColor = UIColor.orangeColor()
    }
    
    /// MARK: - 判断是否是新版本
    /// 判断是否是新版本
    private func isNewVersion() -> Bool {
    // 1. 获取到当前版本
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        print("当前版本: \(currentVersion)")
        // 2. 获取上一次保存的版本
        let sandBoxVersionKey = "sandBoxVersionKey"// 保存到沙盒的key
    let sandBoxVersion = NSUserDefaults.standardUserDefaults().stringForKey(sandBoxVersionKey)
        print("沙盒版本: \(sandBoxVersion)")
        let version = currentVersion != sandBoxVersion
     if version {
    // 3. 保存当前版本
    NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: "sandBoxVersionKey")
        NSUserDefaults.standardUserDefaults().synchronize()
        }
    // 4. 返回,当前版本大于上一次的版本
    return version
     }
    
    /// 判断程序启动后显示的默认控制器
    private func defaultViewController() -> UIViewController {
    // 判断用户是否登录
        if !HOPuserAccount.userLogin {
            // 用户没有登录, 返回HOPMainViewController对象
        return HOPMainController()
            }
    // 用户已经登录
        return isNewVersion() ? HOPNewFeatureController() : HOPWelcomeController()
     }
    
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

